import { _decorator, Collider2D, Component, Contact2DType, instantiate, IPhysics2DContact, Node, Prefab} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Generator')
export class Generator extends Component {
    @property({type:Prefab})
    private monster_1:Prefab = null

    private flag:boolean = false
    start() {
        this.node.getComponent(Collider2D).on(Contact2DType.BEGIN_CONTACT,this.onBeginContact,this)
    }

    update(deltaTime: number) {
        if(this.flag){
            let node = instantiate(this.monster_1)
            let no = this.node.getParent()
            no.addChild(node)
            node.setPosition(0,100,0)
            this.flag = !this.flag
        }
    }
    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null){
        if(otherCollider.group == 2){
            this.flag = !this.flag
        }
    }
}